package com.bible.test.pad.activity.listview;

import java.util.ArrayList;
import java.util.List;

import com.bible.fire.R;
import com.bible.test.bean.MenuEntity;
import com.bible.test.bean.Person;

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ListViewDemo extends Activity {
	
	//定义Person
	List<Person> persons ;
	ListAdapter adapter = null;
	ListView listView01 = null;
	EditText editTextPersonName = null;
	
	//定义上下文菜单  
    final private static int CONTEXT_MENU_EDIT = 0;//菜单[编辑]的编号   
    final private static int CONTEXT_MENU_SHARE = 1;//菜单[分享]的编号   
    final private static int CONTEXT_MENU_DELETE = 2;//菜单[删除]的编号   
    final private static int CONTEXT_MENU_CANCEL = 3;//菜单[取消]的编号 
	private MenuEntity[] contextMenus = {  
            new MenuEntity(0, CONTEXT_MENU_EDIT, 0, "编辑"),  
            new MenuEntity(0, CONTEXT_MENU_SHARE, 1, "分享"),  
            new MenuEntity(0, CONTEXT_MENU_DELETE, 2, "删除"),  
            new MenuEntity(0, CONTEXT_MENU_CANCEL, 3, "取消"),  
            };  

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_view01);
        //获取组件
        listView01 = (ListView)findViewById(R.id.listView01);
        editTextPersonName = (EditText)findViewById(R.id.editText01Edit);
        
        //显示ListView
        initListAllPersons();
        showByMyBaseAdapter();
        
        //初始化ListView的事件
        //initListView01Event();
    }
	
	public void initListAllPersons(){
		persons = new ArrayList<Person>();
		persons.add(new Person(100,"李小龙","香港",android.R.drawable.ic_delete));
		persons.add(new Person(101,"施瓦辛格","美国",android.R.drawable.ic_dialog_alert));
		persons.add(new Person(102,"戴安娜王妃","英国",android.R.drawable.ic_dialog_dialer));
		persons.add(new Person(103,"成龙","香港",android.R.drawable.ic_dialog_map));
		persons.add(new Person(104,"史泰龙","美国",android.R.drawable.ic_lock_idle_alarm));
		persons.add(new Person(105,"圣女贞德","法国",android.R.drawable.ic_media_next));
		persons.add(new Person(106,"列宁","苏联",android.R.drawable.ic_media_play));
		persons.add(new Person(107,"爱迪生","美国",android.R.drawable.ic_menu_add));
		persons.add(new Person(108,"孔子","中国",android.R.drawable.ic_media_rew));
		persons.add(new Person(109,"杨七郎","宋朝",android.R.drawable.ic_menu_zoom));
		persons.add(new Person(110,"秦始皇","秦国",android.R.drawable.ic_menu_agenda));
		persons.add(new Person(111,"岳飞","宋朝",android.R.drawable.ic_lock_lock));
	}
	
	public void showByMyBaseAdapter(){
		adapter = new MyBaseAdapter(this, persons);
		listView01.setAdapter(adapter);
	}

	/**
	 * 按钮button01Edit的onClick事件.
	 * @param view
	 */
	public void editPersonAndRefreshListView01(View view){
		//获取TextEdit数据
		String value = editTextPersonName.getText().toString();
		//更新ListView的内容，并且动态刷新.
		persons.get(0).name=value;
		((BaseAdapter) adapter).notifyDataSetChanged();
	}
	
	
	

	/**
	 * 初始化listView01的事件.
	 */
	public void initListView01Event(){
		
		//ListView的item点击事件
		listView01.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemClickListener]点击了："+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
			}
		});
		
		//ListView的item长按点击事件
		listView01.setOnItemLongClickListener(new OnItemLongClickListener(){

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemLongClickListener]点击了："+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
				return false;
			}
			
		});
		
		//ListView的键盘选中事件(直接触摸屏幕选中不会激发)
		listView01.setOnItemSelectedListener(new OnItemSelectedListener(){

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemSelectedListener:onItemSelected]点击了："
+persons.get(position).name, 
						Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				Toast.makeText(getApplicationContext(), 
						"[OnItemSelectedListener:onNothingSelected]点击了", 
						Toast.LENGTH_SHORT).show();
			}
			
		});
		
		//ListView组件:创建上下文菜单
		listView01.setOnCreateContextMenuListener(new OnCreateContextMenuListener(){
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				//获取上下文菜单点击的ListView的位置(position)属性
				AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
				int position = info.position;
				
				//设置上下文菜单标题
				menu.setHeaderTitle(persons.get(position).name);
				
				//设置上下文菜单栏目
				MenuEntity menuEntity = null;  
                for(int i=0;i<contextMenus.length;i++){  
                    menuEntity = contextMenus[i];  
                    menu.add(menuEntity.getGroupId(),menuEntity.getItemId()
,menuEntity.getOrder(),menuEntity.getTitle());  
                }  
			}
		});
	}

	//监听选择上下文菜单的选择事件
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		//选中的上下文菜单的itemId
		int itemId = item.getItemId();
		//获取上下文菜单点击的ListView的位置(position)属性
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		int position = info.position;
		//Toast
		Toast.makeText(getApplicationContext(), 
				"[onContextItemSelected]点击了"+persons.get(position).name+"" +
						",菜单="+contextMenus[itemId].getTitle(), 
				Toast.LENGTH_SHORT).show();
		return true;
	}
	
}